<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="课程开始时间" prop="courseTimeStart">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.courseTimeStart"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择课程开始时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="课程结束时间" prop="courseTimeEnd">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.courseTimeEnd"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择课程结束时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="报名开始时间" prop="registrationStartTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.registrationStartTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择课程报名开始时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="报名截止时间" prop="registrationEndTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.registrationEndTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择课程报名截止时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="课程方向ID" prop="courseLabelId">-->
<!--        <el-input-->
<!--          v-model="queryParams.courseLabelId"-->
<!--          placeholder="请输入课程方向ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="课程名称" prop="courseName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="课程名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程方向" prop="courseLabelId">
        <el-select
          v-model="queryParams.courseLabelId"
          placeholder="课程方向"
          clearable
          @keyup.enter.native="handleQuery"
        >
          <el-option
            v-for="label in labelList"
            :key="label.id"
            :label="label.labelName"
            :value="label.id"
          />
        </el-select>
        <!--        <el-input-->
        <!--          v-model="queryParams.courseLabelName"-->
        <!--          placeholder="请输入课程方向"-->
        <!--          clearable-->
        <!--          @keyup.enter.native="handleQuery"-->
        <!--        />-->
      </el-form-item>
      <el-form-item label="推荐">
        <el-select v-model="queryParams.recommended" clearable @keyup.enter.native="handleQuery">
          <el-option label="否" value="0"></el-option>
          <el-option label="是" value="1"></el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="是否进入推荐列表" prop="recommended">-->
<!--        <el-input-->
<!--          v-model="queryParams.recommended"-->
<!--          placeholder="请输入是否进入推荐列表"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="是否已发布" prop="publish">-->
<!--        <el-select v-model="queryParams.publish" placeholder="是否已发布" clearable @keyup.enter.native="handleQuery">-->
<!--          <el-option label="否" value="0"></el-option>-->
<!--          <el-option label="是" value="1"></el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="课程是否发布,未发布,已发布" prop="publish">-->
<!--        <el-input-->
<!--          v-model="queryParams.publish"-->
<!--          placeholder="请输入课程是否发布,未发布,已发布"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:course:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:course:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:course:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:course:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="courseList" @selection-change="handleSelectionChange" stripe border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" align="center">
          <template v-slot="scope">
              <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
          </template>
      </el-table-column>
      <el-table-column label="课程名称" align="center" prop="courseName" />
      <el-table-column label="课程费用" align="center" prop="fee" />
      <el-table-column label="类型" align="center" prop="type" >
        <template v-slot="scope">
          <span>{{ scope.row.type==1?"富文本":"链接" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="courseImage" width="100">
        <template v-slot="scope">
          <image-preview :src="scope.row.courseImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="课程状态" align="center" prop="courseStatus" >
        <template v-slot="scope">
          <dict-tag :options="dict.type.hchy_course_status" :value="scope.row.courseStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="课程开始时间" align="center" prop="courseTimeStart" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.courseTimeStart, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="课程结束时间" align="center" prop="courseTimeEnd" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.courseTimeEnd, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="课程报名开始时间" align="center" prop="registrationStartTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.registrationStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="课程报名截止时间" align="center" prop="registrationEndTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.registrationEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="课程方向" align="center" prop="courseLabelName" />
      <el-table-column label="是否进入推荐列表" align="center" prop="recommended" >
        <template v-slot="scope">
          <span>{{ scope.row.recommended==0?"否":"是" }}</span>
        </template>
      </el-table-column>

      <el-table-column label="发布状态" align="center" prop="publish" >
        <template v-slot="scope">
          <el-switch
            v-model="scope.row.publish"
            :active-value="parseInt(1)"
            :inactive-value="parseInt(0)"
            @change="handlePublishChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:course:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:course:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课程对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="课程开始时间" prop="courseTimeStart" label-width="120">
              <el-date-picker clearable
                              v-model="form.courseTimeStart"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择开始时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="课程结束时间" prop="courseTimeEnd" label-width="120">
              <el-date-picker clearable
                              v-model="form.courseTimeEnd"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择结束时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="报名开始时间" prop="registrationStartTime" label-width="120">
              <el-date-picker clearable
                              v-model="form.registrationStartTime"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择开始时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报名截止时间" prop="registrationEndTime" label-width="120">
              <el-date-picker clearable
                              v-model="form.registrationEndTime"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择结束时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="课程名称" prop="courseName">
          <el-input v-model="form.courseName" type="input" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="课程费用" prop="fee">
          <el-input v-model="form.fee" @input="validateAmount" type="input" placeholder="请输入课程费用" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-radio-group v-model="form.type">
            <el-radio
              v-for="dict in dict.type.hchy_activity_type"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="图片">
          <image-upload v-model="form.courseImage" :limit="1"/>
        </el-form-item>
        <el-form-item label="课程方向" prop="courseLabelId">
          <el-select
            v-model="form.courseLabelId"
            placeholder="课程方向"
            clearable
            @keyup.enter.native="handleQuery"
          >
            <el-option
              v-for="label in labelList"
              :key="label.id"
              :label="label.labelName"
              :value="label.id"
            />
          </el-select>
<!--          <el-input v-model="form.courseLabelId" placeholder="请输入课程方向ID" />-->
        </el-form-item>
<!--        <el-form-item label="课程方向" prop="courseLabelName">-->
<!--          <el-input v-model="form.courseLabelName" placeholder="请输入课程方向" />-->
<!--        </el-form-item>-->
        <el-form-item label="是否推荐" prop="recommended">
          <el-radio-group v-model="form.recommended">
            <el-radio
              v-for="dict in dict.type.hchy_recommended_staus"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="发布状态" prop="publish">
          <el-radio-group v-model="form.publish">
            <el-radio
              v-for="dict in dict.type.hchy_publish_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
<!--        <el-form-item label="是否发布" prop="publish">-->
<!--          <el-select v-model="form.publish">-->
<!--            <el-option label="否" :value="0"></el-option>-->
<!--            <el-option label="是" :value="1"></el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <el-form-item v-show="form.type" v-if="form.courseDescription!=undefined" label="课程详情" prop="courseDescription">
<!--          <el-input v-model="form.courseDescription" type="textarea" placeholder="请输入内容" />-->
          <editor v-model="form.courseDescription" :min-height="192"/>
        </el-form-item>
        <el-form-item v-show="!form.type" label="跳转链接" prop="courseUrl">
          <el-input v-model="form.courseUrl" type="text" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { pageListCourse, getCourse, delCourse, addCourse, updateCourse } from "@/api/hchy/course";
import { listLabel } from '@/api/hchy/label'
import { updateActivity } from '@/api/hchy/activity'

export default {
  name: "Course",
  dicts: ['hchy_publish_status', 'hchy_recommended_staus','hchy_activity_type','hchy_course_status'],
  data() {
    return {
      labelList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程表格数据
      courseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        updateByUserName: null,
        createByUserName: null,
        courseName: null,
        type: null,
        courseDescription: null,
        courseTimeStart: null,
        courseTimeEnd: null,
        registrationStartTime: null,
        registrationEndTime: null,
        courseLabelId: null,
        courseLabelName: null,
        recommended: null,
        courseStatus: null,
        publish: null,
        fee: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        courseTimeStart: [
          { required: true, message: "课程开始时间不能为空", trigger: "blur" }
        ],
        courseTimeEnd: [
          { required: true, message: "课程结束时间不能为空", trigger: "blur" }
        ],
        registrationStartTime: [
          { required: true, message: "报名开始时间不能为空", trigger: "blur" }
        ],
        registrationEndTime: [
          { required: true, message: "报名结束时间不能为空", trigger: "blur" }
        ],
        courseName: [
          { required: true, message: "课程名称不能为空", trigger: "blur" }
        ],
        courseLabelId: [
          { required: true, message: "课程方向不能为空", trigger: "blur" }
        ],
        fee: [
          { required: true, message: "课程费用不能为空", trigger: "blur" },
          { validator: this.validateAmount, trigger: 'blur' }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getListLabel();
  },
  methods: {
    /**
     * 发布状态修改
     */
    handlePublishChange(row) {
      console.log('old:', row.publish)
      let text = row.publish == '0' ? "下架" : "发布";
      this.$modal.confirm('确认要"' + text + '""【' + row.courseName + '】"吗？').then(function () {
        return updateCourse(row);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function () {
        row.publish = row.publish === "0" ? "1" : "0";
      });
    },
    validateAmount(rule, value, callback) {
      if (!value) {
        return callback(new Error('金额不能为空'));
      }
      const reg = /^\d+(\.\d{1,2})?$/;
      if (!reg.test(value)) {
        return callback(new Error('请输入有效的金额，最多两位小数'));
      }
      return callback();
    },
    getListLabel(){
      listLabel().then((response=>{
        this.labelList = response.data
      }))
    },
    /**
     * 查询课程列表
     */
    getList() {
      this.loading = true;
      pageListCourse(this.queryParams).then(response => {
        this.courseList = response.data.rows;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /**
     * 取消按钮
     */
    cancel() {
      this.open = false;
      this.reset();
    },
    /**
     * 表单重置
     */
    reset() {
      this.form = {
        id: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        updateByUserName: null,
        createByUserName: null,
        courseName: null,
        type: 1,
        courseDescription: '',
        courseTimeStart: null,
        courseTimeEnd: null,
        registrationStartTime: null,
        registrationEndTime: null,
        courseLabelId: null,
        courseLabelName: null,
        recommended: 0,
        courseStatus: 0,
        publish: 0,
        fee: 0
      };
      this.resetForm("form");
    },
    /**
     * 搜索按钮操作
     */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /**
     * 重置按钮操作
     */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /**
     * 多选框选中数据
     */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /**
     * 新增按钮操作
     */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课程";
    },
    /**
     * 修改按钮操作
     */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCourse(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改课程";
      });
    },
    /**
     * 提交按钮
     */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCourse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCourse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /**
     * 删除按钮操作
     */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除课程编号为"' + ids + '"的数据项？').then(function() {
        return delCourse(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /**
     * 导出按钮操作
     */
    handleExport() {
      this.download('hchy/course/export', {
        ...this.queryParams
      }, `course_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
